$(function() {	
	function initializeViz() {
		var kind = getURLParameter('kind');
		$.ajax({
			url : "/ExternalTable/ticket",
			type : "get",
			data : {
				'kind' : kind,
				'time':new Date().getTime()
			},
			dataType : "json",
			beforeSend : function(request) {
				request.setRequestHeader("Content-Type",
						"application/x-www-form-urlencoded;charset=UTF-8");
				request.setRequestHeader("Access-Control-Allow-Headers",
						"X-Requested-With");
			},
			success : function(result) {
				var ti = result.ticket;
				var book = result.book;
				var sheet = result.sheet;
				console.log(ti);

				var placeholderDiv = document.getElementById("vizContainer");
				//console.log(placeholderDiv);

				//var url = "http://172.20.5.145:8000/views/_13/sheet0";
				//url = "http://172.20.5.145:8000/trusted/" + ti+ "/views/_13/sheet0";
				
				//url = "http://172.20.5.145:8000/trusted/" + ti + "/views/"+ book + "/" + sheet;
				url = "http://172.20.5.203/trusted/" + ti + "/views/"+ book + "/" + sheet;
				console.log(url);

				var options = {
					width : placeholderDiv.offsetWidth,
					height : placeholderDiv.offsetHeight,
					//width : '1000px',
					//height : '500px',
					hideTabs : true,
					hideToolbar : true,
					onFirstInteractive : function() {
						workbook = viz.getWorkbook();
						activeSheet = workbook.getActiveSheet();
					}
				};

				//viz = new tableau.Viz(placeholderDiv, url, options);
				viz = new tableau.Viz(placeholderDiv, url);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				console.log('error:');
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}
	initializeViz();		
});

function getURLParameter(name) {
	return decodeURIComponent((new RegExp('[?|&]' + name + '='
			+ '([^&;]+?)(&|#|;|$)').exec(location.search) || [ , "" ])[1]
			.replace(/\+/g, '%20'))
			|| null;
}